package zhihui.mobile.ui.express;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Display;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Toast;

import com.google.gson.Gson;

import org.json.JSONObject;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import zhihui.mobile.R;
import zhihui.mobile.bean.express.ExpressUnsignedRecordBeanListV2;
import zhihui.mobile.interfaces.net.IHttpCallBackListener;
import zhihui.mobile.manager.express.ExpressManager;

public class ReceiveExpressDetailsDialog {
    @Bind(R.id.express_receive_details_rv)
    RecyclerView expressReceiveDetailsRv;

    private Activity activity;
    private String packageId;
    private LayoutInflater inflater;
    private View view;
    private Dialog shareDialog;
    private ReceiveExpressInAdapter receiveExpressInAdapter;


    public ReceiveExpressDetailsDialog(Activity activity, String packageId) {
        this.activity = activity;
        this.packageId = packageId;
        this.inflater = LayoutInflater.from(activity);
    }


    public void createDialog() {
        view = inflater.inflate(R.layout.dialog_receive_express_details, null);
        shareDialog = new AlertDialog.Builder(activity, R.style.SelectTimeStyle).create();
        shareDialog.show();
        ButterKnife.bind(this, view);
        Window window = shareDialog.getWindow();
        window.setGravity(Gravity.CENTER);
//        window.setWindowAnimations(R.style.ActionSheetDialogAnimation);
        Display display = activity.getWindowManager().getDefaultDisplay();
        WindowManager.LayoutParams layoutParams = window.getAttributes();
        layoutParams.width = (int) (display.getWidth() * 0.8);
        layoutParams.height = (int) (display.getHeight() * 0.8);
        window.setAttributes(layoutParams);
        window.setContentView(view);
        initData();
    }

    private void initData() {
        expressReceiveDetailsRv.setLayoutManager(new LinearLayoutManager(activity));
        expressReceiveDetailsRv.setHasFixedSize(true);

        String s = "{\n" +
                "    \"data\": [\n" +
                "        {\n" +
                "            \"applydate\": \"2017-09-26 15:16:29\",\n" +
                "            \"enddate_f\": \"2017-09-26 15:16:29\",\n" +
                "            \"date_f\": \"2017-09-26 15:16:29\",\n" +
                "            \"status_f\": 1\n" +
                "        },\n" +
                "        {\n" +
                "            \"applydate\": \"2017-09-26 15:16:29\",\n" +
                "            \"enddate_f\": \"2017-09-26 15:16:29\",\n" +
                "            \"date_f\": \"2017-09-26 15:16:29\",\n" +
                "            \"status_f\": 2\n" +
                "        },\n" +
                "        {\n" +
                "            \"applydate\": \"2017-09-26 15:16:29\",\n" +
                "            \"enddate_f\": \"2017-09-26 15:16:29\",\n" +
                "            \"date_f\": \"2017-09-26 15:16:29\",\n" +
                "            \"status_f\": 3\n" +
                "        },\n" +
                "        {\n" +
                "            \"applydate\": \"2017-09-26 15:16:29\",\n" +
                "            \"enddate_f\": \"2017-09-26 15:16:29\",\n" +
                "            \"date_f\": \"2017-09-26 15:16:29\",\n" +
                "            \"status_f\": 4\n" +
                "        }\n" +
                "    ],\n" +
                "    \"msg\": \"\",\n" +
                "    \"state\": \"success\"\n" +
                "}";

        Gson gson = new Gson();
        ExpressUnsignedRecordBeanListV2 expressBeanList = gson.fromJson(s, ExpressUnsignedRecordBeanListV2.class);
        receiveExpressInAdapter = new ReceiveExpressInAdapter(expressBeanList.getData(), activity);
        expressReceiveDetailsRv.setAdapter(receiveExpressInAdapter);


        ExpressManager.getInstance().initExpressUnsignedRecordList(new IHttpCallBackListener() {
            @Override
            public void success(JSONObject result) {

            }

            @Override
            public void failed(String message) {
                Toast.makeText(activity, message, Toast.LENGTH_SHORT).show();
            }
        }, packageId);

    }


    @OnClick(R.id.express_receive_details_close_iv)
    public void onViewClicked() {
        shareDialog.dismiss();
    }
}
